package cn.foxtech.cloud.repo.comp.haas.service;

import cn.foxtech.cloud.core.exception.ServiceException;
import cn.foxtech.cloud.repo.comp.haas.entity.RepoCompHaasEntity;
import cn.foxtech.common.utils.file.FileNameUtils;
import cn.foxtech.common.utils.method.MethodUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Component
public class RepoCompHaasFileService {
    public String getHaasDirName(String absolutePath, RepoCompHaasEntity compEntity) {
        String path = absolutePath + "/repository/haas/" + compEntity.getHardManufacturer() + "/" + compEntity.getHardDeviceType() + "/" + compEntity.getSoftManufacturer() + "/" + compEntity.getSoftDeviceType();
        return FileNameUtils.getOsFilePath(path);
    }

    public String getHaasUrl(RepoCompHaasEntity compEntity, String fileName) {
        return "/repository/haas/" + compEntity.getHardManufacturer() + "/" + compEntity.getHardDeviceType() + "/" + compEntity.getSoftManufacturer() + "/" + compEntity.getSoftDeviceType();
    }

    public String saveFile(MultipartFile multipartFile, String filePath, Long time) throws IOException {
        if (multipartFile.isEmpty()) {
            throw new ServiceException("上传文件为空");
        }

        // 上传方的原始文件名
        String originalFilename = multipartFile.getOriginalFilename();
        // 扩展名
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (MethodUtils.hasEmpty(extName)) {
            throw new ServiceException("上传文件必须为sota文件");
        }
        if (!".sota".equalsIgnoreCase(extName)) {
            throw new ServiceException("上传文件必须为sota文件");
        }

        File dir = new File(filePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        File dest = new File(filePath + "/" + time + extName + ".tmp");
        multipartFile.transferTo(dest);

        return time + extName;
    }

    public void rename(String filePath, String srcFileName, String dstFileName) {
        File src = new File(filePath + "/" + srcFileName);
        File dst = new File(filePath + "/" + dstFileName);
        src.renameTo(dst);
        src.delete();
    }

    public void remove(RepoCompHaasEntity compEntity, String fileName) {
        File dir = new File("");
        String path = this.getHaasDirName(dir.getAbsolutePath(), compEntity);
        File file = new File(path + "/" + fileName);
        file.delete();
    }

    public void cleanResidue(String hardManufacturer, String hardDeviceType, String softManufacturer, String softDeviceType) {
        try {
            RepoCompHaasEntity compEntity = new RepoCompHaasEntity();
            compEntity.setHardManufacturer(hardManufacturer);
            compEntity.setHardDeviceType(hardDeviceType);
            compEntity.setSoftManufacturer(softManufacturer);
            compEntity.setSoftDeviceType(softDeviceType);


            File dir = new File("");
            String modelDir = this.getHaasDirName(dir.getAbsolutePath(), compEntity);

            // 获得本地文件名
            List<String> localList = FileNameUtils.findFileList(modelDir, false, true);

            // 检查：残存的本地垃圾，并删除
            for (String localName : localList) {
                localName = localName.replace("/", "\\");

                // 检查：是否为目录
                File file = new File(localName);
                if (file.isDirectory()) {
                    continue;
                }

                // 檢查：是否為臨時文件
                if (!file.getName().endsWith(".tmp")) {
                    continue;
                }

                // 删除垃圾文件
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
